﻿#define MUTEX

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;
using NeuroCheck.BaseTypes.DeviceDriver;

using CLVisionIOCardNC620.Properties;

using System.Runtime.InteropServices;
using System.Text;
using System.Xml.Serialization;
using Microsoft.Win32;
using CLVisionIOCardNC620.IOCardDevice;
using System.Threading;
using System.Diagnostics;
using System.Web;



namespace CLVisionIOCardNC620
{
    // Token: 0x02000009 RID: 9
    public class CLVisionIOCardDeviceDriver : INcDeviceDriver, IDisposable
    {

        // Token: 0x0600004A RID: 74 RVA: 0x0000288C File Offset: 0x00000A8C
        private CLVisionIOCardDeviceDriver()
        {
            this.m_colOpenedExampleDevices = new Collection<CLVisionIOCardDevice>();
            CLVisionIOCardDeviceDriver.m_LastErrorCode = NcDeviceDriverError.NoError;
            CLVisionIOCardDeviceDriver.m_LastErrorMessage = string.Empty;
        }

        // Token: 0x0600004C RID: 76 RVA: 0x00002902 File Offset: 0x00000B02
        public static CLVisionIOCardDeviceDriver CreateInstance()
        {
            return CLVisionIOCardDeviceDriver.m_driverInstance;
        }

        // Token: 0x17000015 RID: 21
        // (get) Token: 0x0600004D RID: 77 RVA: 0x00002909 File Offset: 0x00000B09
        // (set) Token: 0x0600004E RID: 78 RVA: 0x00002910 File Offset: 0x00000B10
        public static string LastErrorMessage
        {
            get
            {
                return CLVisionIOCardDeviceDriver.m_LastErrorMessage;
            }
            set
            {
                CLVisionIOCardDeviceDriver.m_LastErrorMessage = value;
            }
        }

        // Token: 0x17000016 RID: 22
        // (get) Token: 0x0600004F RID: 79 RVA: 0x00002918 File Offset: 0x00000B18
        // (set) Token: 0x06000050 RID: 80 RVA: 0x0000291F File Offset: 0x00000B1F
        public static NcDeviceDriverError LastErrorCode
        {
            get
            {
                return CLVisionIOCardDeviceDriver.m_LastErrorCode;
            }
            set
            {
                CLVisionIOCardDeviceDriver.m_LastErrorCode = value;
            }
        }

        // Token: 0x06000051 RID: 81 RVA: 0x00002928 File Offset: 0x00000B28
        public bool ControlDriver(NcDeviceDriverControlSelectionCodeInfo selCode, ref object data, ref object dataEx)
        {
            CLVisionIOCardDeviceDriver.LastErrorCode = NcDeviceDriverError.NoError;
            CLVisionIOCardDeviceDriver.LastErrorMessage = string.Empty;
            if (this.m_eventLogAccess != null)
            {
                this.m_eventLogAccess.AddDebugMessage(this, base.GetType().Name + ": ControlDriver called with selection code '" + selCode.ToString() + "'.");
            }
            switch (selCode)
            {
                case NcDeviceDriverControlSelectionCodeInfo.GetDriverVersion:
                    data = Assembly.GetExecutingAssembly().GetName().Version;
                    goto IL_1E4;
                case NcDeviceDriverControlSelectionCodeInfo.GetBillboardBitmap:
                    Bitmap bitmapIOCard = null;
                    using (MemoryStream ms = new MemoryStream(Resources.IOCard))
                    {
                        bitmapIOCard = new Bitmap(ms);
                    }
                    data = bitmapIOCard;
                    goto IL_1E4;

                case NcDeviceDriverControlSelectionCodeInfo.GetCompanyLogoBitmap:
                    Bitmap bitmap = null;
                    using (MemoryStream ms = new MemoryStream(Resources.logo))
                    {
                        bitmap = new Bitmap(ms);
                    }
                    data = bitmap;
                    goto IL_1E4;
                case NcDeviceDriverControlSelectionCodeInfo.GetHelpFileName:
                    data = "IOCard.chm";
                    goto IL_1E4;
                case NcDeviceDriverControlSelectionCodeInfo.CheckDriverVersions:
                    data = new NcDeviceDriverStateReport
                    {
                        OverallDriverResult = NcOverallDriverReportResult.OK
                    };
                    goto IL_1E4;
                case (NcDeviceDriverControlSelectionCodeInfo)5:
                case (NcDeviceDriverControlSelectionCodeInfo)6:
                case (NcDeviceDriverControlSelectionCodeInfo)7:
                case (NcDeviceDriverControlSelectionCodeInfo)8:
                case (NcDeviceDriverControlSelectionCodeInfo)9:
                case (NcDeviceDriverControlSelectionCodeInfo)15:
                case (NcDeviceDriverControlSelectionCodeInfo)16:
                case (NcDeviceDriverControlSelectionCodeInfo)17:
                case (NcDeviceDriverControlSelectionCodeInfo)18:
                case (NcDeviceDriverControlSelectionCodeInfo)19:
                    break;
                case NcDeviceDriverControlSelectionCodeInfo.SetDriverConfigurationFile:
                case NcDeviceDriverControlSelectionCodeInfo.SetPnPNotificationsInterface: // No plug and play events --> do nothing
                case NcDeviceDriverControlSelectionCodeInfo.SetProjectDirectoryServiceInterface:
                case NcDeviceDriverControlSelectionCodeInfo.GetCameraSetupFileDescriptions:
                case NcDeviceDriverControlSelectionCodeInfo.GetCameraSetupFileExtensions:
                    goto IL_1E4;
                case NcDeviceDriverControlSelectionCodeInfo.SetEventLogAccessInterface:
                    this.m_eventLogAccess = (data as INcDeviceEventLogAccess);
                    if (this.m_eventLogAccess == null)
                    {
                        CLVisionIOCardDeviceDriver.LastErrorCode = NcDeviceDriverError.ErrInvalidParameter;
                        CLVisionIOCardDeviceDriver.LastErrorMessage = string.Format("Event log access is of type {0} where {1} is exspected.", data.GetType().Name, typeof(INcDeviceEventLogAccess).Name);
                        goto IL_1E4;
                    }
                    goto IL_1E4;
                case NcDeviceDriverControlSelectionCodeInfo.CheckLicense:
                    data = new NcDeviceDriverLicenseDesc
                    {
                        IsRunningInDemoMode = false
                    };
                    goto IL_1E4;
                case NcDeviceDriverControlSelectionCodeInfo.GetCameraSetupFileFormatCount:
                    data = 0;
                    goto IL_1E4;
                default:
                    switch (selCode)
                    {
                        case NcDeviceDriverControlSelectionCodeInfo.Reserved1:
                        case NcDeviceDriverControlSelectionCodeInfo.Reserved2:
                        case NcDeviceDriverControlSelectionCodeInfo.Reserved3:
                        case NcDeviceDriverControlSelectionCodeInfo.Reserved4:
                        case NcDeviceDriverControlSelectionCodeInfo.Reserved5:
                            goto IL_1E4;
                    }
                    break;
            }

            CLVisionIOCardDeviceDriver.LastErrorCode = NcDeviceDriverError.ErrFunctionNotAvailable;
            CLVisionIOCardDeviceDriver.LastErrorMessage = "ControlDriver doesn't support the selection code \"" + selCode.ToString() + "\".";
            if (this.m_eventLogAccess != null)
            {
                this.m_eventLogAccess.AddWarning(this, base.GetType().Name + ": " + CLVisionIOCardDeviceDriver.LastErrorMessage);
            }
        IL_1E4:
            if (this.m_eventLogAccess != null && CLVisionIOCardDeviceDriver.LastErrorCode == NcDeviceDriverError.NoError)
            {
                this.m_eventLogAccess.AddDebugMessage(this, base.GetType().Name + ": ControlDriver executed successfully with selection code '" + selCode.ToString() + "'.");
            }
            return CLVisionIOCardDeviceDriver.LastErrorCode == NcDeviceDriverError.NoError;
        }

        // Token: 0x06000052 RID: 82 RVA: 0x00002B64 File Offset: 0x00000D64
        public bool GetDeviceCount(out int deviceCount)
        {
            CLVisionIOCardDeviceDriver.LastErrorCode = NcDeviceDriverError.NoError;
            CLVisionIOCardDeviceDriver.LastErrorMessage = string.Empty;
            deviceCount = 0;
            bool releaseDriver = (m_iIOCardDeviceDriver == null);
            if (true == releaseDriver)
            {
                try
                {
                    m_iIOCardDeviceDriver = EntryPoint.CreateDriver();
                    deviceCount = m_iIOCardDeviceDriver.GetDeviceCount();
                }
                catch (Exception ex)
                {
                    // -----------------------------------
                    // Exception handling
                    // -----------------------------------
                    LastErrorCode = NcDeviceDriverError.ErrDriverInit;
                    LastErrorMessage = "Error \"" + ex.Message
                                     + "\" accessing low level driver.";

                    // Is the event log access valid?
                    if (m_eventLogAccess != null)
                    {
                        // Write warning to event log
                        m_eventLogAccess.AddWarning(this, this.GetType().Name
                                                    + ": GetDeviceCount: " + LastErrorMessage);

                    } // end of The event log access is valid

                    // Return an invalid number
                    return false;

                } // end of try ... catch
                finally
                {
                    //获取到数据后释放
                    // if (true == releaseDriver)
                    // {
                    m_iIOCardDeviceDriver.Release();
                    m_iIOCardDeviceDriver = null;

                    //  }
                }
            } // end of true == releaseDriver
            return true;
        }

        // Token: 0x06000053 RID: 83 RVA: 0x00002C2C File Offset: 0x00000E2C
        public bool GetDeviceInfo(int index, out string alias, out string deviceName, out string serialNumber)
        {
            CLVisionIOCardDeviceDriver.LastErrorCode = NcDeviceDriverError.NoError;
            CLVisionIOCardDeviceDriver.LastErrorMessage = string.Empty;
            // Return empty names
            alias = string.Empty;
            deviceName = string.Empty;
            serialNumber = string.Empty;

            bool releaseDriver = (m_iIOCardDeviceDriver == null);
            if (true == releaseDriver)
            {
                try
                {
                    m_iIOCardDeviceDriver = EntryPoint.CreateDriver();

                    IIOCardDevice iOCardDevice = m_iIOCardDeviceDriver.GetDevice(index);
                    deviceName = iOCardDevice.TypeName;
                    serialNumber = iOCardDevice.nCurCardID.ToString();
                    alias = deviceName.Replace(' ', '_') + "_" + serialNumber;
                }
                catch (Exception ex)
                {
                    // -----------------------------------
                    // Exception handling
                    // -----------------------------------
                    LastErrorCode = NcDeviceDriverError.ErrDriverInit;
                    LastErrorMessage = "Error \"" + ex.Message
                                     + "\" accessing low level driver.";

                    // Is the event log access valid?
                    if (m_eventLogAccess != null)
                    {
                        // Write warning to event log
                        m_eventLogAccess.AddWarning(this, this.GetType().Name
                                                    + ": GetDeviceInfo: " + LastErrorMessage);

                    } // end of The event log access is valid

                    return false;

                } // end of try ... catch
                finally
                {
                    //获取参数后  释放
                    //if (true == releaseDriver)
                    //{
                    m_iIOCardDeviceDriver.Release();
                    m_iIOCardDeviceDriver = null;
            deviceName = iOCardDevice.TypeName;
            serialNumber = iOCardDevice.SerialNumber;
            alias = deviceName.Replace(' ', '_') + "_" + serialNumber;

            if (true == releaseDriver)
            {
                m_iIOCardDeviceDriver.Release();
                m_iIOCardDeviceDriver = null;
            deviceName = iOCardDevice.TypeName;
            serialNumber = iOCardDevice.SerialNumber;
            alias = deviceName.Replace(' ', '_') + "_" + serialNumber;

            if (true == releaseDriver)
            {
                m_iIOCardDeviceDriver.Release();
                m_iIOCardDeviceDriver = null;

                    //} // end of true == releaseDriver

                }
            } // end of true == releaseDriver

            return true;
        }


        // Token: 0x06000054 RID: 84 RVA: 0x00002D40 File Offset: 0x00000F40
        public bool OpenDevice(string alias, out int tempDeviceId)
        {
            CLVisionIOCardDeviceDriver.LastErrorCode = NcDeviceDriverError.NoError;
            CLVisionIOCardDeviceDriver.LastErrorMessage = string.Empty;


                    //获取参数失败后  释放
                    //if (true == releaseDriver)
                    //{
                    m_iIOCardDeviceDriver.Release();
                    m_iIOCardDeviceDriver = null;

                    //} // end of true == releaseDriver
               
            bool releaseDriver = (m_iIOCardDeviceDriver == null);
            if (releaseDriver)
            {
                try
                {
                    this.m_iIOCardDeviceDriver = EntryPoint.CreateDriver();
                }
                catch (Exception ex)
                {
                    CLVisionIOCardDeviceDriver.LastErrorCode = NcDeviceDriverError.ErrDriverInit;




                    CLVisionIOCardDeviceDriver.LastErrorMessage = "Error \"" + ex.Message + "\" accessing low level driver.";
                    if (this.m_eventLogAccess != null)
                    {
                        this.m_eventLogAccess.AddWarning(this, base.GetType().Name + ": OpenDevice: " + CLVisionIOCardDeviceDriver.LastErrorMessage);
                    }
                    tempDeviceId = -1;
                    return false;
                }
               
            }
            IIOCardDevice iIOCardDevice = null;

            for (int i = 0; i < this.m_iIOCardDeviceDriver.GetDeviceCount(); i++)
            {
                iIOCardDevice = this.m_iIOCardDeviceDriver.GetDevice(i);

                int index = alias.LastIndexOf('_');
                int ID = int.Parse(alias.Substring(index + 1));

                if (ID == iIOCardDevice.nCurCardID)
                {
#if MUTEX
                    //创建板卡互斥体
                    Mutex mutex = new Mutex(true, iIOCardDevice.TypeName + iIOCardDevice.nCurCardID, out bool createdNew);
                    if (!createdNew)
                    {
                        tempDeviceId = -1;
                        MessageBox.Show("该IO板卡已被加载！");
                   
                        return false;
                    }
                    else
                    {
                        //IO卡处于打开状态，返回
                        if (iIOCardDevice.IsOpen)
                        {
                            //绑定程序进程关闭事件
                            AppDomain.CurrentDomain.ProcessExit += (sender, e) =>
                            {
                                foreach (var key in mutexes.Keys)
                                {
                                    mutexes[key].WaitOne();
                                    mutexes[key].ReleaseMutex();
                                    mutexes[key].Dispose();
                                }
                            };
                            mutexes.Add(iIOCardDevice.TypeName + iIOCardDevice.nCurCardID, mutex);
                                mutexes.Add(iIOCardDevice.TypeName + iIOCardDevice.nCurCardID, mutex);
                            }
                        }
                    }

                    break;
#else
                    //IO卡处于打开状态，返回
                    if (iIOCardDevice.IsOpen)
                    {
                        break;
                    }

                    break;
#else
                    //IO卡处于打开状态，返回
                    if (iIOCardDevice.IsOpen)
                    {
                        break;
                    }
                  
                    break;
#endif
                }
            }


            int TypeID = TransIDToTypeID(iIOCardDevice.nCurCardID, iIOCardDevice.TypeName);

            if (iIOCardDevice == null)
            {
                CLVisionIOCardDeviceDriver.LastErrorCode = NcDeviceDriverError.ErrDeviceNotAvailable;
                CLVisionIOCardDeviceDriver.LastErrorMessage = "The device with the alias \"" + alias + "\" is unavailable.";
                if (this.m_eventLogAccess != null)
                {
                    this.m_eventLogAccess.AddWarning(this, base.GetType().Name + ": OpenDevice: " + CLVisionIOCardDeviceDriver.LastErrorMessage);
                }
                tempDeviceId = -1;
           
                return false;
            }
                CLVisionIOCardDeviceDriver.LastErrorMessage = "The device with the alias \"" + alias + "\" is unavailable.";
            int TypeID = TransIDToTypeID(iIOCardDevice.nCurCardID, iIOCardDevice.TypeName);

            this.m_temporaryDeviceID = TypeID;
            CLVisionIOCardDevice item = new CLVisionIOCardDevice(this.m_temporaryDeviceID, iIOCardDevice);
            this.m_colOpenedExampleDevices.Add(item);
            tempDeviceId = TypeID;


            //获取参数后  释放
            m_iIOCardDeviceDriver.Release();
            m_iIOCardDeviceDriver = null;

       
            return true;
                return false;
            }
                CLVisionIOCardDeviceDriver.LastErrorMessage = "The device with the alias \"" + alias + "\" is unavailable.";
            this.m_temporaryDeviceID = Convert.ToInt32(iIOCardDevice.SerialNumber);
            CLVisionIOCardDevice item = new CLVisionIOCardDevice(this.m_temporaryDeviceID, iIOCardDevice);
            this.m_colOpenedExampleDevices.Add(item);
            tempDeviceId = this.m_temporaryDeviceID;
            return true;
                return false;
            }

            this.m_temporaryDeviceID = Convert.ToInt32(iIOCardDevice.SerialNumber);
            CLVisionIOCardDevice item = new CLVisionIOCardDevice(this.m_temporaryDeviceID, iIOCardDevice);
            this.m_colOpenedExampleDevices.Add(item);
            tempDeviceId = this.m_temporaryDeviceID;
            return true;
        }


        private int TransIDToTypeID(int id, string typeName)
        {
            //P16从0     P32 10    E16  20     E32  30

            switch (typeName)
            {
                case "P16":
                    return id;
                case "E16":
                    return 10 + id;
                case "P32":
                    return 20 + id;
                case "E32":
                    return 30 + id;
                case "IO32":
                    return 40 + id;
                default:
                    return -1;
            }

        }
        private int TransTypeIDToID(int id, string typeName)
        {
            //P16从0     P32 10    E16  20     E32  30

            switch (typeName)
            {
                case "P16":
                    return id;
                case "E16":
                    return id - 10;
                case "P32":
                    return id - 20;
                case "E32":
                    return id - 30;
                case "IO32":
                    return id - 40;
                default:
                    return -1;
            }

        }

        // Token: 0x06000055 RID: 85 RVA: 0x00002ED4 File Offset: 0x000010D4
        public bool CloseDevice(int tempDeviceId)
        {
           
            deviceByID.IIOCardDevice.CloseIODevice(this.m_temporaryDeviceID, out int ErrorCode, out string ErrorText);
            CLVisionIOCardDeviceDriver.LastErrorMessage = string.Empty;
            CLVisionIOCardDevice deviceByID = this.GetDeviceByID(tempDeviceId);
            this.m_temporaryDeviceID = TransTypeIDToID(tempDeviceId, deviceByID.IIOCardDevice.TypeName);
            if (deviceByID == null)
            {
                CLVisionIOCardDeviceDriver.LastErrorCode = NcDeviceDriverError.ErrDeviceNotOpen;
                CLVisionIOCardDeviceDriver.LastErrorMessage = "A device with temporary device ID of \"" + this.m_temporaryDeviceID.ToString() + "\" is not open currently.";
            deviceByID.IIOCardDevice.CloseIODevice(tempDeviceId, out int ErrorCode, out string ErrorText);
                {
                    this.m_eventLogAccess.AddWarning(this, base.GetType().Name + ": CloseDevice: " + CLVisionIOCardDeviceDriver.LastErrorMessage);
                }
        
                return false;
            }

            deviceByID.IIOCardDevice.CloseIODevice(tempDeviceId, out int ErrorCode, out string ErrorText);




   
            //释放指定板卡的互斥体
#if MUTEX
            string mutexName = deviceByID.IIOCardDevice.TypeName + deviceByID.IIOCardDevice.nCurCardID;
            mutexes[mutexName].WaitOne();
            mutexes[mutexName].ReleaseMutex();
            mutexes[mutexName].Dispose();
            mutexes.Remove(mutexName);
#endif
        
            this.m_colOpenedExampleDevices.Remove(deviceByID);

            if (this.m_colOpenedExampleDevices.Count == 0)
            {
                this.m_iIOCardDeviceDriver.Release();
                this.m_iIOCardDeviceDriver = null;
                this.m_temporaryDeviceID = TransTypeIDToID(tempDeviceId, deviceByID.IIOCardDevice.TypeName);
            }
            return true;
        }

        // Token: 0x06000056 RID: 86 RVA: 0x00002F98 File Offset: 0x00001198
        public bool ControlDevice(int tempDeviceId, NcDeviceControlSelectionCodeInfo selCode, ref object data, ref object dataEx)
        {
            CLVisionIOCardDeviceDriver.LastErrorCode = NcDeviceDriverError.NoError;
            CLVisionIOCardDeviceDriver.LastErrorMessage = string.Empty;
            if (this.m_eventLogAccess != null)
            {
                this.m_eventLogAccess.AddDebugMessage(this, base.GetType().Name + ": ControlDevice called with selection code '" + selCode.ToString() + "'.");
            }
            try
            {
                CLVisionIOCardDevice deviceByID = this.GetDeviceByID(tempDeviceId);


                switch (selCode)
                {
                    case NcDeviceControlSelectionCodeInfo.GetDeviceSettingsString:

                        return true;
                    case NcDeviceControlSelectionCodeInfo.GetDeviceDefaultSettingsString:

                        goto IL_230;
                    case NcDeviceControlSelectionCodeInfo.GetDevicePropertyNames://3
                        string[] array = PropertyNameConstants.Names;
                        data = array;
                        goto IL_230;
                    case NcDeviceControlSelectionCodeInfo.GetDevicePropertyType:
                        deviceByID.GetPropertyType(data, out dataEx);
                        goto IL_230;
                    case NcDeviceControlSelectionCodeInfo.GetDevicePropertyValue:
                        deviceByID.GetPropertyValue(data, out dataEx);
                        goto IL_230;
                    case NcDeviceControlSelectionCodeInfo.GetDevicePropertyMinimum:
                        goto IL_230;
                    case NcDeviceControlSelectionCodeInfo.GetDevicePropertyMaximum:
                        goto IL_230;
                    case NcDeviceControlSelectionCodeInfo.GetDevicePropertyPossibleValues: //8
                        deviceByID.GetPropertyPossibleValues(data, out dataEx);
                        goto IL_230;
                    case NcDeviceControlSelectionCodeInfo.GetDeviceMethodNames: //9--Control Device
                        data = deviceByID.MethodNames;
                        goto IL_230;
                    case NcDeviceControlSelectionCodeInfo.SetDevicePropertyValue: //20
                        deviceByID.SetPropertyValue(data, dataEx);
                        goto IL_230;
                    case NcDeviceControlSelectionCodeInfo.CommandDeviceInit: //40
                                                                             //string text = (string)data;
                                                                             //先关闭再打开
                        if (deviceByID.IIOCardDevice.IsOpen == true)
                        {
                            if (!deviceByID.IIOCardDevice.CloseIODevice(this.m_temporaryDeviceID, out _, out _))
                            {
                                return false;
                            }
                            if (!deviceByID.IIOCardDevice.OpenIODevice(this.m_temporaryDeviceID, out _, out _))
                            {
                                return false;
                            }
                        }
                        else
                        {
                            if (!deviceByID.IIOCardDevice.OpenIODevice(this.m_temporaryDeviceID, out _, out _))
                            {
                                return false;
                            }
                        }
                        goto IL_230;
                    case NcDeviceControlSelectionCodeInfo.CommandDeviceReset:
                        deviceByID.SetParameterToDefault();
                        goto IL_230;
                    case NcDeviceControlSelectionCodeInfo.CommandDeviceExecuteMethod://42--Control Device
                        bool bIsOk = deviceByID.Execute(this.m_temporaryDeviceID, data, dataEx);
                        goto IL_230;

                    case NcDeviceControlSelectionCodeInfo.GetIoInputCaps: //3000

                        data = deviceByID.IIOCardDevice.IOCardInputBits;
                        goto IL_230;
                    case NcDeviceControlSelectionCodeInfo.GetIoOutputCaps: //3001
                        data = deviceByID.IIOCardDevice.IOCardOutputBits;
                        goto IL_230;
                    case NcDeviceControlSelectionCodeInfo.CommandIoGetInputByte: //3100
                        {
                            bool nRet = deviceByID.IIOCardDevice.GetInPutData(this.m_temporaryDeviceID, out long InputData, out _, out _);
                            if (nRet)
                            {
                                data = (int)InputData;
                                goto IL_230;
                            }
                            else
                            {
                                data = null;
                                break;
                            }
                        }

                    case NcDeviceControlSelectionCodeInfo.CommandIoGetOutputByte: //3101
                        {
                            bool nRet = deviceByID.IIOCardDevice.GetOutPutData(this.m_temporaryDeviceID, out long OutputData, out _, out _);
                            if (nRet)
                            {
                                data = OutputData;
                                goto IL_230;
                            }
                            else
                            {
                                data = null;
                                break;
                            }

                        }
                    case NcDeviceControlSelectionCodeInfo.CommandIoSetOutputByte: //3102
                        {
                            byte OutputData = (byte)data;
                            bool nRet = deviceByID.IIOCardDevice.SetOutPutData(this.m_temporaryDeviceID, OutputData, out _, out _);
                            goto IL_230;

                        }
                    case NcDeviceControlSelectionCodeInfo.CommandIoGetInputBit: //3103
                        {
                            bool bBitStatus = (bool)data;
                            int nInBitNo = (int)dataEx;
                            bool Res;
                            Res = deviceByID.IIOCardDevice.ReadInPutbit(this.m_temporaryDeviceID, nInBitNo, out int InputData, out _, out _);
                            if (InputData == 1)
                                data = false;
                            else
                                data = true;
                        }
                        goto IL_230;

                    case NcDeviceControlSelectionCodeInfo.CommandIoSetOutputBit: //3104
                        bool bBitStatusO = (bool)data;
                        int nBitStatus = 0;
                        if (bBitStatusO == false)
                            nBitStatus = 1;
                        int nInBitNoO = (int)dataEx;
                        deviceByID.IIOCardDevice.WriteOutPutBit(this.m_temporaryDeviceID, nInBitNoO, nBitStatus, out _, out _);

                        goto IL_230;
                    case NcDeviceControlSelectionCodeInfo.CommandIoResetAllBits: //3105
                        bool nRets = deviceByID.IIOCardDevice.SetOutPutData(this.m_temporaryDeviceID, 0xFF, out _, out _);
                        if (nRets)
                            break;
                        goto IL_230;
                    case NcDeviceControlSelectionCodeInfo.Reserved1:
                    case NcDeviceControlSelectionCodeInfo.Reserved2:
                    case NcDeviceControlSelectionCodeInfo.Reserved3:
                    case NcDeviceControlSelectionCodeInfo.Reserved4:
                    case NcDeviceControlSelectionCodeInfo.Reserved5:
                        goto IL_230;
                    default:
                        break;
                }//eof switch

                CLVisionIOCardDeviceDriver.LastErrorCode = NcDeviceDriverError.ErrFunctionNotAvailable;
                CLVisionIOCardDeviceDriver.LastErrorMessage = string.Format("The {0} doesn't support the requested function \"{1}\".", base.GetType().Name, selCode.ToString());
                return false;
            IL_230:;
            }
            catch (Exception ex)
            {
                CLVisionIOCardDeviceDriver.LastErrorCode = NcDeviceDriverError.ErrGeneral;
                CLVisionIOCardDeviceDriver.m_LastErrorMessage = ex.Message;
            }
            if (this.m_eventLogAccess != null)
            {
                if (CLVisionIOCardDeviceDriver.LastErrorCode == NcDeviceDriverError.NoError)
                {
                    this.m_eventLogAccess.AddDebugMessage(this, base.GetType().Name + ": ControlDevice executed successfully with selection code '" + selCode.ToString() + "'.");
                }
                else
                {
                    this.m_eventLogAccess.AddDebugMessage(this, string.Concat(new string[]
                    {
                        base.GetType().Name,
                        ": During execution of ControlDevice with selection code '",
                        selCode.ToString(),
                        "' the error '",
                        CLVisionIOCardDeviceDriver.m_LastErrorMessage,
                        "' occured."
                    }));
                }
            }
            return CLVisionIOCardDeviceDriver.LastErrorCode == NcDeviceDriverError.NoError;
        }

        // Token: 0x06000057 RID: 87 RVA: 0x000032B0 File Offset: 0x000014B0
        public bool GetLastError(out NcDeviceDriverError err, out string errMsg)
        {
            err = CLVisionIOCardDeviceDriver.m_LastErrorCode;
            errMsg = CLVisionIOCardDeviceDriver.m_LastErrorMessage;
            if (this.m_eventLogAccess != null)
            {
                this.m_eventLogAccess.AddDebugMessage(this, base.GetType().Name + ": GetLastError executed successfully.");
            }
            return CLVisionIOCardDeviceDriver.m_LastErrorCode != NcDeviceDriverError.NoError;
        }


        // Token: 0x0600005E RID: 94 RVA: 0x000046BC File Offset: 0x000028BC
        public bool ShowDeviceTestDialog(int tempDeviceId, int subDeviceIndex)
        {
            CLVisionIOCardDeviceDriver.LastErrorCode = NcDeviceDriverError.NoError;
            CLVisionIOCardDeviceDriver.LastErrorMessage = string.Empty;
            CLVisionIOCardDevice deviceByID = this.GetDeviceByID(tempDeviceId);

            int nRet = deviceByID.TestDigitize();
            if (nRet > 0)
            {
                MessageBox.Show("No error. Test was successful!!");
                CLVisionIOCardDeviceDriver.LastErrorCode = NcDeviceDriverError.NoError;
                return true;
            }
            MessageBox.Show("Error!", "Test was failed!!");
            CLVisionIOCardDeviceDriver.LastErrorCode = NcDeviceDriverError.ErrInvalidParameter;
            return false;
        }

        // Token: 0x0600005F RID: 95 RVA: 0x00004757 File Offset: 0x00002957
        public void ShowDriverAboutDialog()
        {
            CLVisionIOCardDeviceDriver.LastErrorCode = NcDeviceDriverError.NoError;
            CLVisionIOCardDeviceDriver.LastErrorMessage = string.Empty;
            AssemblyName asmName = Assembly.GetExecutingAssembly().GetName();

            string filePath = Assembly.GetExecutingAssembly().Location;
            FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(filePath);
            MessageBox.Show("Version: " + fileVersionInfo.FileVersion + "\r\n"
                            + "IOCard NeuroCheck Driver.",
                            "About " + asmName.Name,
                            MessageBoxButtons.OK);
        }

        // Token: 0x06000060 RID: 96 RVA: 0x00004784 File Offset: 0x00002984
        public bool ControlSubDevice(int tempDeviceId, int subDeviceIndex, NcDeviceControlSelectionCodeInfo selCode, ref object data, ref object dataEx)
        {
            CLVisionIOCardDeviceDriver.m_LastErrorCode = NcDeviceDriverError.ErrNotImplemented;
            CLVisionIOCardDeviceDriver.m_LastErrorMessage = "The method 'ControlSubDevice' is not supported by this driver.";
            return false;
        }

        // Token: 0x06000061 RID: 97 RVA: 0x0000479C File Offset: 0x0000299C
        public void Dispose()
        {
            CLVisionIOCardDeviceDriver.LastErrorCode = NcDeviceDriverError.NoError;
            CLVisionIOCardDeviceDriver.LastErrorMessage = string.Empty;
            foreach (CLVisionIOCardDevice exampleDevice in this.m_colOpenedExampleDevices)
            {
                exampleDevice.Dispose();
            }
            this.m_colOpenedExampleDevices.Clear();

            //释放所有互斥量
#if MUTEX
            foreach (var key in mutexes.Keys)
            {
                mutexes[key].WaitOne();
                mutexes[key].ReleaseMutex();
                mutexes[key].Dispose();
            }
            mutexes.Clear();
#endif

            if (this.m_iIOCardDeviceDriver != null)
            {
                this.m_iIOCardDeviceDriver.Release();
                this.m_iIOCardDeviceDriver = null;
            }



        }

        private CLVisionIOCardDevice GetDeviceByID(int tempDeviceId)
        {
            foreach (CLVisionIOCardDevice exampleDevice in this.m_colOpenedExampleDevices)
            {
                if (tempDeviceId == exampleDevice.TempDeviceId)
                {
                    return exampleDevice;
                }
            }
            return null;
        }

        public bool ShowDevicePropertyDialog(int tempDeviceId, int subDeviceIndex, out bool isDialogCancelled)
        {
            isDialogCancelled = false;
            return true;
        }

        // Token: 0x04000016 RID: 22
        private static CLVisionIOCardDeviceDriver m_driverInstance = new CLVisionIOCardDeviceDriver();

        // Token: 0x04000017 RID: 23
        private int m_temporaryDeviceID;

        Dictionary<string, Mutex> mutexes = new Dictionary<string, Mutex>();


        // Token: 0x04000019 RID: 25
        private IOCardDeviceDriver m_iIOCardDeviceDriver;

        // Token: 0x04000020 RID: 32
        private INcDeviceEventLogAccess m_eventLogAccess;

        // Token: 0x04000021 RID: 33
        private static string m_LastErrorMessage;

        // Token: 0x04000022 RID: 34
        private static NcDeviceDriverError m_LastErrorCode;

        //板卡对象互斥体集合
        Dictionary<string, Mutex> mutexes = new Dictionary<string, Mutex>();



    }
}
